<template>
  <view class="container">
    <!-- 根据状态显示不同内容 -->
    <view v-if="!showQuestionnaire">
      <!-- 主内容区域 -->
        <view class="content">
          <!-- 问候语 -->
          <view class="greeting">
            <text class="greeting-text">Hi! {{greetingName}}{{loginStatus && loginStatus.userType === 'doctor' ? ' 医生' : ''}}</text>
            <!-- 已移除头像图片 -->
          </view>

          <!-- 焦虑量表图片 -->
          <view class="scale-image-container">
            <image class="scale-image" src="/images/焦虑量表.jpg" mode="aspectFill"></image>
          </view>

          <!-- 欢迎信息 -->
        <view class="welcome-text">
          <text class="welcome-title">欢迎进入，
汉密尔焦虑量表测试！</text>
          <text class="welcome-desc">我们将为您提供三种版本选择
您可以分别选择 17项、21项、24项
的不同版本量表提问
为了保证结果的准确性
请您滑动选项条根据来访者状态评分</text>
        </view>

        <!-- 用户协议 -->
        <view class="agreement">
          <view class="checkbox-container" @click="toggleAgreement">
            <view class="checkbox" :class="{ 'checked': isAgreed }"></view>
          </view>
          <text class="agreement-text">我已阅读并且同意用户协议</text>
        </view>

        <!-- 开始按钮 -->
        <button class="start-button" :disabled="!isAgreed" @click="startAssessment">开始作答</button>
      </view>
    </view>

    <!-- 问卷部分 -->
    <view v-else class="questionnaire">
    <!-- 顶部标题 -->
    <view class="questionnaire-header">
      <text class="questionnaire-title">汉密尔顿焦虑量表</text>
      <view class="page-indicator">
        <view class="page-number" :class="{ 'active': currentPage === 1 }">1</view>
        <view class="page-number" :class="{ 'active': currentPage === 2 }">2</view>
      </view>
      <view class="save-button">
        <image src="/static/logo.png" class="save-icon"></image>
      </view>
    </view>

    <!-- 问卷内容 -->
    <view class="questionnaire-content">
      <!-- 已从系统获取患者信息 -->

      <!-- 测试项目 -->
      <view class="test-items">
        <!-- 第一页：项目1-7 -->
        <view v-if="currentPage === 1">
          <!-- 项目1 -->
          <view class="test-item">
            <text class="item-title">1.焦虑心境</text>
            <view class="score-options">
              <radio-group @change="changeScore(0, $event)">
                <label class="score-option"><radio color="#13c2c2" :value="0" :checked="scores[0] === 0" />0分：无症状</label>
                <label class="score-option"><radio color="#13c2c2" :value="1" :checked="scores[0] === 1" />1分：轻度焦虑，不影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="2" :checked="scores[0] === 2" />2分：中等焦虑，对日常生活有一定影响</label>
                <label class="score-option"><radio color="#13c2c2" :value="3" :checked="scores[0] === 3" />3分：严重焦虑，明显影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="4" :checked="scores[0] === 4" />4分：极度焦虑，几乎无法进行日常生活</label>
              </radio-group>
            </view>
          </view>

          <!-- 项目2 -->
          <view class="test-item">
            <text class="item-title">2.紧张</text>
            <view class="score-options">
              <radio-group @change="changeScore(1, $event)">
                <label class="score-option"><radio color="#13c2c2" :value="0" :checked="scores[1] === 0" />0分：无症状</label>
                <label class="score-option"><radio color="#13c2c2" :value="1" :checked="scores[1] === 1" />1分：轻度紧张，不影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="2" :checked="scores[1] === 2" />2分：中等紧张，对日常生活有一定影响</label>
                <label class="score-option"><radio color="#13c2c2" :value="3" :checked="scores[1] === 3" />3分：严重紧张，明显影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="4" :checked="scores[1] === 4" />4分：极度紧张，几乎无法进行日常生活</label>
              </radio-group>
            </view>
          </view>

          <!-- 项目3 -->
          <view class="test-item">
            <text class="item-title">3.害怕</text>
            <view class="score-options">
              <radio-group @change="changeScore(2, $event)">
                <label class="score-option"><radio color="#13c2c2" :value="0" :checked="scores[2] === 0" />0分：无症状</label>
                <label class="score-option"><radio color="#13c2c2" :value="1" :checked="scores[2] === 1" />1分：轻度害怕，不影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="2" :checked="scores[2] === 2" />2分：中等害怕，对日常生活有一定影响</label>
                <label class="score-option"><radio color="#13c2c2" :value="3" :checked="scores[2] === 3" />3分：严重害怕，明显影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="4" :checked="scores[2] === 4" />4分：极度害怕，几乎无法进行日常生活</label>
              </radio-group>
            </view>
          </view>

          <!-- 项目4 -->
          <view class="test-item">
            <text class="item-title">4.失眠</text>
            <view class="score-options">
              <radio-group @change="changeScore(3, $event)">
                <label class="score-option"><radio color="#13c2c2" :value="0" :checked="scores[3] === 0" />0分：无症状</label>
                <label class="score-option"><radio color="#13c2c2" :value="1" :checked="scores[3] === 1" />1分：轻度失眠，不影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="2" :checked="scores[3] === 2" />2分：中等失眠，对日常生活有一定影响</label>
                <label class="score-option"><radio color="#13c2c2" :value="3" :checked="scores[3] === 3" />3分：严重失眠，明显影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="4" :checked="scores[3] === 4" />4分：极度失眠，几乎无法进行日常生活</label>
              </radio-group>
            </view>
          </view>

          <!-- 项目5 -->
          <view class="test-item">
            <text class="item-title">5.认知功能</text>
            <view class="score-options">
              <radio-group @change="changeScore(4, $event)">
                <label class="score-option"><radio color="#13c2c2" :value="0" :checked="scores[4] === 0" />0分：无症状</label>
                <label class="score-option"><radio color="#13c2c2" :value="1" :checked="scores[4] === 1" />1分：轻度认知障碍，不影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="2" :checked="scores[4] === 2" />2分：中等认知障碍，对日常生活有一定影响</label>
                <label class="score-option"><radio color="#13c2c2" :value="3" :checked="scores[4] === 3" />3分：严重认知障碍，明显影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="4" :checked="scores[4] === 4" />4分：极度认知障碍，几乎无法进行日常生活</label>
              </radio-group>
            </view>
          </view>

          <!-- 项目6 -->
          <view class="test-item">
            <text class="item-title">6.抑郁心境</text>
            <view class="score-options">
              <radio-group @change="changeScore(5, $event)">
                <label class="score-option"><radio color="#13c2c2" :value="0" :checked="scores[5] === 0" />0分：无症状</label>
                <label class="score-option"><radio color="#13c2c2" :value="1" :checked="scores[5] === 1" />1分：轻度抑郁，不影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="2" :checked="scores[5] === 2" />2分：中等抑郁，对日常生活有一定影响</label>
                <label class="score-option"><radio color="#13c2c2" :value="3" :checked="scores[5] === 3" />3分：严重抑郁，明显影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="4" :checked="scores[5] === 4" />4分：极度抑郁，几乎无法进行日常生活</label>
              </radio-group>
            </view>
          </view>

          <!-- 项目7 -->
          <view class="test-item">
            <text class="item-title">7.躯体性焦虑-肌肉系统</text>
            <view class="score-options">
              <radio-group @change="changeScore(6, $event)">
                <label class="score-option"><radio color="#13c2c2" :value="0" :checked="scores[6] === 0" />0分：无症状</label>
                <label class="score-option"><radio color="#13c2c2" :value="1" :checked="scores[6] === 1" />1分：轻度肌肉紧张，不影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="2" :checked="scores[6] === 2" />2分：中等肌肉紧张，对日常生活有一定影响</label>
                <label class="score-option"><radio color="#13c2c2" :value="3" :checked="scores[6] === 3" />3分：严重肌肉紧张，明显影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="4" :checked="scores[6] === 4" />4分：极度肌肉紧张，几乎无法进行日常生活</label>
              </radio-group>
            </view>
          </view>
        </view>

        <!-- 第二页：项目8-14 -->
        <view v-if="currentPage === 2">
          <!-- 项目8 -->
          <view class="test-item">
            <text class="item-title">8.躯体性焦虑-感觉系统</text>
            <view class="score-options">
              <radio-group @change="changeScore(7, $event)">
                <label class="score-option"><radio color="#13c2c2" :value="0" :checked="scores[7] === 0" />0分：无症状</label>
                <label class="score-option"><radio color="#13c2c2" :value="1" :checked="scores[7] === 1" />1分：轻度感觉异常，不影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="2" :checked="scores[7] === 2" />2分：中等感觉异常，对日常生活有一定影响</label>
                <label class="score-option"><radio color="#13c2c2" :value="3" :checked="scores[7] === 3" />3分：严重感觉异常，明显影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="4" :checked="scores[7] === 4" />4分：极度感觉异常，几乎无法进行日常生活</label>
              </radio-group>
            </view>
          </view>

          <!-- 项目9 -->
          <view class="test-item">
            <text class="item-title">9.心血管系统症状</text>
            <view class="score-options">
              <radio-group @change="changeScore(8, $event)">
                <label class="score-option"><radio color="#13c2c2" :value="0" :checked="scores[8] === 0" />0分：无症状</label>
                <label class="score-option"><radio color="#13c2c2" :value="1" :checked="scores[8] === 1" />1分：轻度心血管症状，不影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="2" :checked="scores[8] === 2" />2分：中等心血管症状，对日常生活有一定影响</label>
                <label class="score-option"><radio color="#13c2c2" :value="3" :checked="scores[8] === 3" />3分：严重心血管症状，明显影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="4" :checked="scores[8] === 4" />4分：极度心血管症状，几乎无法进行日常生活</label>
              </radio-group>
            </view>
          </view>

          <!-- 项目10 -->
          <view class="test-item">
            <text class="item-title">10.呼吸系统症状</text>
            <view class="score-options">
              <radio-group @change="changeScore(9, $event)">
                <label class="score-option"><radio color="#13c2c2" :value="0" :checked="scores[9] === 0" />0分：无症状</label>
                <label class="score-option"><radio color="#13c2c2" :value="1" :checked="scores[9] === 1" />1分：轻度呼吸系统症状，不影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="2" :checked="scores[9] === 2" />2分：中等呼吸系统症状，对日常生活有一定影响</label>
                <label class="score-option"><radio color="#13c2c2" :value="3" :checked="scores[9] === 3" />3分：严重呼吸系统症状，明显影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="4" :checked="scores[9] === 4" />4分：极度呼吸系统症状，几乎无法进行日常生活</label>
              </radio-group>
            </view>
          </view>

          <!-- 项目11 -->
          <view class="test-item">
            <text class="item-title">11.胃肠道症状</text>
            <view class="score-options">
              <radio-group @change="changeScore(10, $event)">
                <label class="score-option"><radio color="#13c2c2" :value="0" :checked="scores[10] === 0" />0分：无症状</label>
                <label class="score-option"><radio color="#13c2c2" :value="1" :checked="scores[10] === 1" />1分：轻度胃肠道症状，不影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="2" :checked="scores[10] === 2" />2分：中等胃肠道症状，对日常生活有一定影响</label>
                <label class="score-option"><radio color="#13c2c2" :value="3" :checked="scores[10] === 3" />3分：严重胃肠道症状，明显影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="4" :checked="scores[10] === 4" />4分：极度胃肠道症状，几乎无法进行日常生活</label>
              </radio-group>
            </view>
          </view>

          <!-- 项目12 -->
          <view class="test-item">
            <text class="item-title">12.生殖泌尿系统症状</text>
            <view class="score-options">
              <radio-group @change="changeScore(11, $event)">
                <label class="score-option"><radio color="#13c2c2" :value="0" :checked="scores[11] === 0" />0分：无症状</label>
                <label class="score-option"><radio color="#13c2c2" :value="1" :checked="scores[11] === 1" />1分：轻度生殖泌尿系统症状，不影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="2" :checked="scores[11] === 2" />2分：中等生殖泌尿系统症状，对日常生活有一定影响</label>
                <label class="score-option"><radio color="#13c2c2" :value="3" :checked="scores[11] === 3" />3分：严重生殖泌尿系统症状，明显影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="4" :checked="scores[11] === 4" />4分：极度生殖泌尿系统症状，几乎无法进行日常生活</label>
              </radio-group>
            </view>
          </view>

          <!-- 项目13 -->
          <view class="test-item">
            <text class="item-title">13.植物神经系统症状</text>
            <view class="score-options">
              <radio-group @change="changeScore(12, $event)">
                <label class="score-option"><radio color="#13c2c2" :value="0" :checked="scores[12] === 0" />0分：无症状</label>
                <label class="score-option"><radio color="#13c2c2" :value="1" :checked="scores[12] === 1" />1分：轻度植物神经症状，不影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="2" :checked="scores[12] === 2" />2分：中等植物神经症状，对日常生活有一定影响</label>
                <label class="score-option"><radio color="#13c2c2" :value="3" :checked="scores[12] === 3" />3分：严重植物神经症状，明显影响日常生活</label>
                <label class="score-option"><radio color="#13c2c2" :value="4" :checked="scores[12] === 4" />4分：极度植物神经症状，几乎无法进行日常生活</label>
              </radio-group>
            </view>
          </view>

          <!-- 项目14 -->
          <view class="test-item">
            <text class="item-title">14.会谈时行为表现</text>
            <view class="score-options">
              <radio-group @change="changeScore(13, $event)">
                <label class="score-option"><radio color="#13c2c2" :value="0" :checked="scores[13] === 0" />0分：无症状</label>
                <label class="score-option"><radio color="#13c2c2" :value="1" :checked="scores[13] === 1" />1分：轻度异常行为，不影响会谈</label>
                <label class="score-option"><radio color="#13c2c2" :value="2" :checked="scores[13] === 2" />2分：中等异常行为，对会谈有一定影响</label>
                <label class="score-option"><radio color="#13c2c2" :value="3" :checked="scores[13] === 3" />3分：严重异常行为，明显影响会谈</label>
                <label class="score-option"><radio color="#13c2c2" :value="4" :checked="scores[13] === 4" />4分：极度异常行为，几乎无法进行会谈</label>
              </radio-group>
            </view>
          </view>
        </view>
      </view>

      <!-- 导航按钮 -->
      <view class="navigation-buttons">
        <!-- 第一页不显示上一页按钮 -->
        <button class="nav-button" @click="prevPage" v-if="currentPage > 1">上一页</button>
        <!-- 第二页不显示下一页按钮 -->
        <button class="nav-button" @click="nextPage" v-if="currentPage < 2" style="margin-left: auto; margin-right: auto;">下一页</button>
      </view>

      <!-- 提交按钮 - 只在第二页显示 -->
      <button class="submit-button" @click="submitTest" v-if="currentPage === 2">测试提交</button>
      
      <!-- 确认勾选 - 只在第二页显示 -->
       <view class="confirm-checkbox" v-if="currentPage === 2">
         <checkbox :checked="confirmSubmit" @tap="confirmSubmit = !confirmSubmit" />确认提交即同意评估结果
       </view>
    </view>
  </view>
</view>
</template>

<script>
import { getLoginStatus } from '../../../utils/auth.js';
import { createAssessment, submitHama } from '../../../utils/api/doctor.js';

export default {
  data() {
    return {
      isAgreed: false,
      showQuestionnaire: false,
      currentPage: 1,
      totalPages: 2,
      confirmSubmit: false,
      loginStatus: null,
      greetingName: '用户',
      // 初始化14个项目的分数为null（标准汉密尔顿焦虑量表）
      scores: Array(14).fill(null),
      assessmentId: null, // 评估记录ID
      patientId: null // 患者ID
    };
  },
  onLoad(options) {
    // 页面加载时获取登录状态和用户信息
    this.loginStatus = getLoginStatus();
    
    // 接收评估ID或患者ID
    if (options.assessmentId) {
      this.assessmentId = parseInt(options.assessmentId);
      console.log('接收到评估ID:', this.assessmentId);
    }
    if (options.patientId) {
      this.patientId = parseInt(options.patientId);
      console.log('接收到患者ID:', this.patientId);
    }
    
    // 提取用户姓名用于问候，按优先级获取：wechat_name > nickname > name > realName
    if (this.loginStatus.userInfo) {
      this.greetingName = this.loginStatus.userInfo.wechat_name || 
                         this.loginStatus.userInfo.nickname || 
                         this.loginStatus.userInfo.name || 
                         this.loginStatus.userInfo.realName || 
                         '用户';
      console.log('焦虑量表页面用户信息:', this.loginStatus.userInfo);
      console.log('设置的问候姓名:', this.greetingName);
    }
    
    // 如果没有传患者ID，使用当前用户ID（患者自己填写）
    if (!this.patientId && this.loginStatus.userInfo) {
      this.patientId = this.loginStatus.userInfo.id;
    }
  },
  methods: {
    // 返回上一页
    navigateBack() {
      uni.navigateBack();
    },
    
    // 切换用户协议同意状态
    toggleAgreement() {
      this.isAgreed = !this.isAgreed;
    },
    
    // 开始测试
    startAssessment() {
      if (this.isAgreed) {
        // 显示问卷部分
        this.showQuestionnaire = true;
      }
    },
    
    // 上一页
    prevPage() {
      if (this.currentPage > 1) {
        // 先滚动到顶部
        this.scrollToTop();
        // 然后再切换页面
        this.currentPage--;
      }
    },
    
    // 下一页
    nextPage() {
      // 检查当前页是否所有项目都已评分
      const startIndex = (this.currentPage - 1) * 7;
      const endIndex = startIndex + 7;
      const currentPageScores = this.scores.slice(startIndex, endIndex);
      const allScored = currentPageScores.every(score => score !== null);
      
      if (!allScored) {
        uni.showToast({
          title: '请完成当前页所有项目的评分',
          icon: 'none'
        });
        return;
      }
      
      if (this.currentPage < this.totalPages) {
        // 先滚动到顶部
        this.scrollToTop();
        // 然后再切换页面
        this.currentPage++;
      } else {
        // 如果已经是最后一页，点击下一页可以跳转到查看报告页面
        this.navigateToReport();
      }
    },
    
    // 滚动到页面顶部
    scrollToTop() {
      // 直接执行滚动操作，不使用setTimeout延迟
      uni.pageScrollTo({
        scrollTop: 0,
        duration: 0 // 无滚动动画，直接跳转
      });
    },
    
    // 提交测试
    async submitTest() {
    // 检查是否所有项目都已评分
    const allScored = this.scores.every(score => score !== null);
    
    if (!allScored) {
      uni.showToast({
        title: '请完成所有项目的评分',
        icon: 'none'
      });
      return;
    }
    
    if (!this.confirmSubmit) {
      uni.showToast({
        title: '请确认提交即同意评估结果',
        icon: 'none'
      });
      return;
    }
    
    try {
      const totalScore = this.calculateTotalScore();
      
      // 判断是否为患者自评（没有传入patientId，或patientId等于当前用户）
      const currentUserId = this.loginStatus.userInfo?.id;
      const isSelfAssessment = !this.patientId || this.patientId === currentUserId;
      
      if (isSelfAssessment) {
        // ========== 患者自评：提交到后端 ==========
        console.log('患者自评模式，提交到后端');

        uni.showLoading({ title: '提交中...' });

        // 1. 创建评估记录
        const assessmentRes = await createAssessment(
          currentUserId,
          new Date().toISOString().split('T')[0],
          0, // hamd_total
          totalScore, // hama_total
          0  // ymrs_total
        );

        if (assessmentRes.code !== 200) {
          throw new Error(assessmentRes.msg || '创建评估记录失败');
        }

        const newAssessmentId = assessmentRes.data.assessment_id;
        console.log('患者自评记录创建成功，ID:', newAssessmentId);

        // 2. 提交HAMA量表详情
        const hamaData = {
          item1_irritability: this.scores[0],
          item2_tension: this.scores[1],
          item3_fears: this.scores[2],
          item4_insomnia: this.scores[3],
          item5_cognition: this.scores[4],
          item6_depressed_mood: this.scores[5],
          item7_somatic_muscle: this.scores[6],
          item8_somatic_sensory: this.scores[7],
          item9_cardiovascular: this.scores[8],
          item10_respiratory: this.scores[9],
          item11_gastrointestinal: this.scores[10],
          item12_genitourinary: this.scores[11],
          item13_autonomic: this.scores[12],
          item14_behavior: this.scores[13]
        };

        const submitRes = await submitHama(newAssessmentId, hamaData);

        if (submitRes.code !== 200) {
          throw new Error(submitRes.msg || '提交量表失败');
        }

        console.log('患者自评HAMA量表提交成功');

        // 3. 保存到本地（用于离线查看）
        const testData = {
          scores: this.scores,
          totalScore: totalScore,
          testType: 'anxiety',
          testDate: new Date().toLocaleString('zh-CN'),
          assessmentId: newAssessmentId,
          isLocal: false
        };
        uni.setStorageSync('anxietyTestData', testData);

        uni.hideLoading();
        uni.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 1500
        });

        setTimeout(() => {
          this.navigateToReport();
        }, 1500);
        
      } else {
        // ========== 医生评估：提交到后端 ==========
        uni.showLoading({ title: '提交中...' });
        
        // 1. 如果没有评估ID，先创建评估记录
        if (!this.assessmentId) {
          console.log('医生评估模式，创建评估记录，患者ID:', this.patientId);
          const assessmentRes = await createAssessment(
            this.patientId,
            new Date().toISOString().split('T')[0],
            0, // hamd_total
            totalScore, // hama_total
            0  // ymrs_total
          );
          
          if (assessmentRes.code !== 200) {
            throw new Error(assessmentRes.msg || '创建评估记录失败');
          }
          
          this.assessmentId = assessmentRes.data.assessment_id;
          console.log('评估记录创建成功，ID:', this.assessmentId);
        }
        
        // 2. 提交HAMA量表详情（14题）
        const hamaData = {
          item1_irritability: this.scores[0],
          item2_tension: this.scores[1],
          item3_fears: this.scores[2],
          item4_insomnia: this.scores[3],
          item5_cognition: this.scores[4],
          item6_depressed_mood: this.scores[5],
          item7_somatic_muscle: this.scores[6],
          item8_somatic_sensory: this.scores[7],
          item9_cardiovascular: this.scores[8],
          item10_respiratory: this.scores[9],
          item11_gastrointestinal: this.scores[10],
          item12_genitourinary: this.scores[11],
          item13_autonomic: this.scores[12],
          item14_behavior: this.scores[13]
        };
        
        console.log('提交HAMA数据:', hamaData);
        const submitRes = await submitHama(this.assessmentId, hamaData);
        
        if (submitRes.code !== 200) {
          throw new Error(submitRes.msg || '提交量表失败');
        }
        
        console.log('HAMA量表提交成功');
        
        // 3. 同时保存到本地（用于离线查看）
        const testData = {
          scores: this.scores,
          totalScore: totalScore,
          testType: 'anxiety',
          testDate: new Date().toLocaleString('zh-CN'),
          assessmentId: this.assessmentId,
          isLocal: false
        };
        uni.setStorageSync('anxietyTestData', testData);
        
        uni.hideLoading();
        uni.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 1500
        });
        
        // 4. 跳转到查看报告页面
        setTimeout(() => {
          this.navigateToReport();
        }, 1500);
      }
      
    } catch (error) {
      uni.hideLoading();
      console.error('提交量表失败:', error);
      uni.showToast({
        title: error.message || '提交失败，请重试',
        icon: 'none',
        duration: 2000
      });
    }
  },
    
    // 计算总分
    calculateTotalScore() {
      return this.scores.reduce((total, score) => {
        return total + (score || 0);
      }, 0);
    },
    
    // 改变评分
    changeScore(index, e) {
      this.scores[index] = parseInt(e.detail.value);
    },
    
    // 跳转到查看报告页面
    navigateToReport() {
      uni.switchTab({
        url: '/pages/chaKanBaoGao/chaKanBaoGao'
      });
    }
  }
};
</script>

<style scoped>
.container {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    background-color: #ffffff;
  }



/* 主内容区域 */
.content {
  flex: 1;
  padding: 30rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* 问候语 */
.greeting {
  display: flex;
  align-items: center;
  margin-bottom: 30rpx;
}
.greeting-text {
  font-size: 32rpx;
  color: #333;
  margin-right: 10rpx;
}
.avatar {
  width: 40rpx;
  height: 40rpx;
  border-radius: 50%;
}

/* 量表图片样式 */
    .scale-image-container {
    width: 100%;
    display: flex;
    justify-content: center;
    margin: 30rpx 0;
  }
  
  .scale-image {
    width: 100%;
    height: 300rpx;
    border-radius: 20rpx;
  }

/* 卡通图片 */
.illustration {
  width: 600rpx;
  height: 300rpx;
  background-color: #e6f7ee;
  border-radius: 20rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 30rpx;
}
.dog-image {
  width: 200rpx;
  height: 200rpx;
}

/* 欢迎信息 */
.welcome-text {
  text-align: left;
  margin-bottom: 60rpx;
}
.welcome-title {
  font-size: 40rpx;
  font-weight: bold;
  color: #333;
  display: block;
  margin-bottom: 20rpx;
}
.welcome-desc {
  font-size: 28rpx;
  color: #666;
  line-height: 50rpx;
}

/* 用户协议 */
.agreement {
  display: flex;
  align-items: center;
  margin-bottom: 40rpx;
  width: 100%;
  justify-content: flex-start;
}
.checkbox-container {
  margin-right: 15rpx;
}
.checkbox {
  width: 30rpx;
  height: 30rpx;
  border: 2rpx solid #ccc;
  border-radius: 6rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}
.checkbox.checked {
  background-color: #13c2c2;
  border-color: #13c2c2;
}

.checkbox.checked::after {
  content: '✓';
  color: #fff;
  font-size: 24rpx;
  font-weight: bold;
}
  .agreement-text {
    font-size: 28rpx;
    color: #13c2c2;
  }

/* 开始按钮 */
  .start-button {
  width: 600rpx;
  height: 90rpx;
  line-height: 90rpx;
  background-color: #13c2c2;
  color: #fff;
  font-size: 36rpx;
  border-radius: 45rpx;
  margin-top: 40rpx;
}
.start-button[disabled] {
  background-color: #ccc;
  color: #fff;
}

/* 问卷样式 */
.questionnaire {
  background-color: #f8f8f8;
  min-height: 100vh;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 10;
}

.questionnaire-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20rpx 30rpx;
  background-color: #ffffff;
  color: #333;
  border-bottom: 1rpx solid #eee;
}

.questionnaire-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
}

.page-indicator {
  display: flex;
  align-items: center;
}

.page-number {
  width: 40rpx;
  height: 40rpx;
  border-radius: 50%;
  background-color: #eee;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 10rpx;
  font-size: 24rpx;
}

.page-number.active {
  background-color: #13c2c2;
  color: #fff;
}

.save-icon {
  width: 40rpx;
  height: 40rpx;
}

.questionnaire-content {
  padding: 30rpx;
  background-color: #ffffff;
}

.patient-info {
  background-color: #fff;
  padding: 30rpx;
  border-radius: 20rpx;
  margin-bottom: 30rpx;
}

.section-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 20rpx;
  display: block;
}

.info-item {
  display: flex;
  align-items: center;
  margin-bottom: 20rpx;
}

.info-label {
  font-size: 28rpx;
  color: #333;
  width: 120rpx;
}

.info-input {
  flex: 1;
  height: 80rpx;
  border: 1rpx solid #ddd;
  border-radius: 10rpx;
  padding: 0 20rpx;
  font-size: 28rpx;
}

.radio-group {
  display: flex;
  align-items: center;
}

.radio-label {
  display: flex;
  align-items: center;
  margin-right: 40rpx;
  font-size: 28rpx;
}

.test-items {
  background-color: #fff;
  padding: 30rpx;
  border-radius: 20rpx;
  margin-bottom: 30rpx;
}

.test-item {
  margin-bottom: 30rpx;
}

.item-title {
    font-size: 30rpx;
    font-weight: bold;
    color: #333;
    margin-bottom: 20rpx;
    display: block;
  }

  .score-options {
    display: flex;
    flex-direction: column;
  }

  .score-option {
    display: flex;
    align-items: center;
    padding: 20rpx;
    margin-bottom: 10rpx;
    background-color: #ffffff;
    border-radius: 10rpx;
    font-size: 28rpx;
  }

  /* 单选按钮颜色 */
  radio .wx-radio-input.wx-radio-input-checked {
    background-color: #13c2c2 !important;
    border-color: #13c2c2 !important;
  }
  
  radio .wx-radio-input.wx-radio-input-checked::before {
    color: #fff !important;
  }

.navigation-buttons {
    display: flex;
    justify-content: space-between;
    margin: 30rpx 0;
  }

  .nav-button {
    width: 280rpx;
    height: 80rpx;
    line-height: 80rpx;
    background-color: #13c2c2;
    color: #fff;
    font-size: 28rpx;
    border-radius: 40rpx;
  }

.submit-button {
  width: 100%;
  height: 90rpx;
  line-height: 90rpx;
  background-color: #13c2c2;
  color: #fff;
  font-size: 36rpx;
  border-radius: 45rpx;
  margin-bottom: 20rpx;
}

.confirm-checkbox {
    text-align: center;
    font-size: 28rpx;
    color: #666;
  }
</style>